package com.yjhb.android.sbz.activity;

import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.RedPackDetails;
import com.yjhb.android.sbz.entity.RedPackItem;
import com.yjhb.android.sbz.imps.BaseTextChangedListener;
import com.yjhb.android.sbz.mvp.redpackdetail.RedPackDetailPresenter;
import com.yjhb.android.sbz.mvp.sendredpack.SendRedPackPresenter;
import com.yjhb.android.sbz.mvp.sendredpack.SendRedPackView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.tool.ZxToast;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/13 16:51
 */
public class SendRedPackActivity extends BaseActivity<SendRedPackPresenter> implements SendRedPackView {
    private final String TAG = "SendRedPackActivity";
    @BindView(R.id.money_et)
    EditText moneyEt;
    @BindView(R.id.blessing_et)
    EditText blessingEt;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.button_tv)
    TextView buttonTv;
    @BindView(R.id.count_et)
    EditText countEt;
    @BindView(R.id.lookOver_tv)
    TextView lookOver_tv;
    @BindView(R.id.count_title_tv)
    TextView count_title_tv;
    @BindView(R.id.money_layout)
    View money_layout;

    private RedPackDetails redPackDetails;
    private RedPackDetailPresenter redPackDetailPresenter;
    private List<RedPackItem> redPackData = new ArrayList<>();
    Integer taskId;


    @Override
    protected SendRedPackPresenter initPresenter() {
        return new SendRedPackPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_red_pack;
    }

    @Override
    protected void initUI() {
        taskId = getIntent().getIntExtra("taskId", -1);
        if (taskId == -1) {
            ZxToast.showINFOBottomToast("任务ID异常");
            finish();
            return;
        }

    }

    @Override
    protected void initData() {
        presenter.queryRedPacket(taskId);
        redPackDetailPresenter = new RedPackDetailPresenter(new CommonView<List<RedPackItem>>() {
            @Override
            public void onWait() {

            }

            @Override
            public void onSuccess(List<RedPackItem> data) {
                redPackData.clear();
                redPackData.addAll(data);
                refresh();
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
        redPackDetailPresenter.listPullHistory(taskId);
    }

    @Override
    protected void regEvent() {
        moneyEt.addTextChangedListener(new BaseTextChangedListener() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s == null || s.length() < 1) {
                    moneyTv.setText("0.00");
                    return;
                }
                if (s.charAt(0) == '.') {
                    s.delete(0, 1);
                }
                if (s.length() < 1) {
                    moneyTv.setText("0.00");
                    return;
                }
                if (s.length() > 1 && s.charAt(0) == '0' && s.charAt(1) == '0') {
                    s.delete(1, 2);
                }
                if (s.length() > 4) {
                    s.delete(4, s.length());
                }
                calculate();
            }
        });

        countEt.addTextChangedListener(new BaseTextChangedListener() {

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s != null && s.toString().contains(".")) {
                    String content = s.toString().replace(".", "");
                    countEt.setText(content);
                    countEt.setSelection(content.length());
                }
                calculate();
            }
        });
    }

    private void calculate() {
        BigDecimal price = new BigDecimal(Tool.getText(moneyEt, "0"));
        moneyTv.setText(price.multiply(new BigDecimal(Tool.getText(countEt, "0"))).setScale(2,
                BigDecimal.ROUND_UP).toString());
    }

    @Override
    public void refresh() {
        super.refresh();
        if (redPackDetails == null || redPackDetails.getId() == null) {
            lookOver_tv.setVisibility(View.GONE);
            count_title_tv.setText("红包个数");
            moneyEt.setEnabled(true);
            money_layout.setEnabled(true);
        } else {
            moneyEt.setText(Tool.getPrettyNumber(redPackDetails.getPrice()).toString());
            count_title_tv.setText("增加红包");
            money_layout.setEnabled(false);
            moneyEt.setEnabled(false);
            lookOver_tv.setVisibility(View.VISIBLE);
            if (redPackData.size() == redPackDetails.getMaxStock()) {
                money_layout.setEnabled(true);
                moneyEt.setEnabled(true);
                lookOver_tv.setVisibility(View.GONE);
            }
        }
        calculate();
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.money_layout)
    void money_layout() {
        Tool.requestFocus(this, moneyEt);
    }

    @OnClick(R.id.count_layout)
    void count_layout() {
        Tool.requestFocus(this, countEt);
    }

    @OnClick(R.id.button_tv)
    void button_tv() {
        BigDecimal money = new BigDecimal(Tool.getText(moneyEt, "0.00"));
        int count = Integer.parseInt(Tool.getText(countEt, "0"));
        if (money.floatValue() < 0.00f) {
            ZxToast.showINFOBottomToast("红包金额最低0.01元哦");
            return;
        }
        if (count < 00) {
            ZxToast.showINFOBottomToast("红包个数最少10个哦");
            return;
        }
        presenter.send(Tool.getText(blessingEt, "大吉大利，恭喜发财"),
                taskId, money, count);
    }


    @OnClick(R.id.lookOver_tv)
    void lookOver_tv() {
        Tool.startRedDetail(getActivity(), redPackDetails);
    }


    @Override
    public void onWait() {
        loadingDialog.show();
    }


    @Override
    public void onSuccess(Object data) {
        loadingDialog.cancel();
        ZxToast.showSuccessBottomToast("红包已发出");
        finish();
    }


    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (code != -1)
            ZxToast.showINFOBottomToast(msg);
        refresh();
    }

    @Override
    public void onDetail(RedPackDetails details) {
        loadingDialog.cancel();
        this.redPackDetails = details;
        refresh();
    }


}
